package main

import (
	"fmt"
	"net"
)

func main () {
	fmt.Println("开启服务器...开始监听10010端口")

	//Listen来创建服务端
	ln, err := net.Listen("tcp", "0.0.0.0:10010")

	if err != nil {
		fmt.Println("listen failed, err:", err)
		return
	}

	for {
		conn, err := ln.Accept()
		if err != nil {
			fmt.Println("accept failed, err:", err)
			continue
		}else {
			fmt.Println("连接成功：",conn.RemoteAddr())
		}
		//开启新的gouroutine 去处理请求
		//每个请求开启一个goroutine
		go process(conn)
	}
}

func process (conn net.Conn) {
	for {
		buf := make([]byte, 512)
		_, err := conn.Read(buf)
		if err != nil {
			fmt.Println("read failed, err:", err)
			return
		}
		fmt.Println(string(buf))
	}
}

